package com.luoqy.speedy.modular.api.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luoqy.speedy.config.UpdateFictionConfig;
import com.luoqy.speedy.data.DataBase;
import com.luoqy.speedy.data.MySqldbUtil;
import com.luoqy.speedy.modular.web.service.WriteFictionService;
import com.luoqy.speedy.util.HttpApi;
import com.luoqy.speedy.util.Result;
@RestController
@RequestMapping("/api/fiction")
public class H5FictionController {
	@Autowired
	private WriteFictionService writeFiction;
	
	/**
	 * 根据ID查询书籍信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/findFictionById")
	public Result findFictionById(int id){
		Result result=new Result();
		Object data=MySqldbUtil.mysqlSelect("select *,(select min(id) from chapter where chapter.fiction_id=fiction.id) news,(select chapter_title from chapter where chapter.id=news) name from fiction where id="+id, "none",new DataBase());
		if(null==data){
			result.setData(data);
			result.setMessage("不存在的书籍");
			result.setState(0);
		}else{
			result.setData(data);
			result.setMessage("查询成功");
			result.setState(1);
		}
		return result;
	}
	/**
	 * 同步更新销售内容库
	 * @return
	 */
	@RequestMapping("/updateSyn")
	public Result updateSaveFiction(){
		Result result=new Result();
		UpdateFictionConfig update=new UpdateFictionConfig();
		update.saveBaseFiction();
		result.setState(1);
		result.setMessage("小说内容进行同步更新存储中");
		result.setData(null);
		return result;
	}
	/**
	 * 遍历该类型下从页码开始的所有小说
	 * @param type 类型
	 * @param page 页码
	 * @return
	 */
	@RequestMapping("/pollfiction")
	public Result pollfiction(String type,int page){
		Result result=new Result();
		try {
			writeFiction.poll(type, page, writeFiction);
			result.setData(null);
			result.setMessage("提交成功");
			result.setState(1);
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			result.setMessage(e.getMessage());
			result.setData(e);
			result.setState(1);
			return result;
		}
	}
	@RequestMapping("/urlFiction")
	public Result urlFiction(String url){
		Result result=new Result();
		try {
			Future<String> future=writeFiction.insertResult("http://www.shuquge.com"+url);
			if("success".equals(future.get().toString())){
				result.setData(null);
				result.setMessage("获取成功");
				result.setState(1);
			}else{
				result.setData(null);
				result.setMessage("获取失败");
				result.setState(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
			result.setData(e);
			result.setMessage("获取失败");
			result.setState(0);
		}
		return result;
	}
	/**
	 * 根据小说名更新小说或添加小说
	 * @param name
	 * @return
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/namefiction")
	public Result namefiction(String name) throws Exception{
		Result result=new Result();
		Document document = Jsoup.parse(HttpApi.post("http://www.shuquge.com/search.php?searchkey="+URLEncoder.encode(name,"utf8")));
		 Elements elements=document.select(".bookname");
		 if(elements.size()==0){
			 result.setData(null);
			 result.setMessage("不存在的小说");
			 result.setState(0);
		 }else if(elements.size()>1){
			 List<Map<String,String>> list=new ArrayList<Map<String,String>>();
			 for (Element element : elements) {
				 Map<String,String> ele=new HashMap<String,String>();
				 String href=element.select("a").attr("href");
				 String text=element.select("a").text();
				 ele.put("href", href);
				 ele.put("text", text);
				 list.add(ele);
			}
			 result.setData(list);
			 result.setMessage("有多本书籍存在，请选择想要的书籍");
			 result.setState(1);
		 }else{
			 String url="http://www.shuquge.com"+elements.get(0).select("a").attr("href");
			 if("success".equals(writeFiction.insertResult(url).get())){
				 result.setData(null);
				 result.setMessage("加载成功");
				 result.setState(1);
			 }else{
				 result.setData(null);
				 result.setMessage("错误的地址");
				 result.setState(0);
			 }
		 }
		return result;
	}
	
}
